home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / tcpdump-3.0 / print-ntp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-15  |  6.6 KB  |  286 lines

  1. /*
  2.  * Copyright (c) 1990, 1991, 1992, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * Format and print ntp packets.
  22.  *    By Jeffrey Mogul/DECWRL
  23.  *    loosely based on print-bootp.c
  24.  */
  25.  
  26. #ifndef lint
  27. static char rcsid[] =
  28.     "@(#) $Header: print-ntp.c,v 1.14 94/06/14 20:18:46 leres Exp $ (LBL)";
  29. #endif
  30.  
  31. #include <sys/param.h>
  32. #include <sys/time.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35.  
  36. #include <net/if.h>
  37.  
  38. #include <netinet/in.h>
  39. #include <netinet/if_ether.h>
  40.  
  41. #include <ctype.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44.  
  45. #include "interface.h"
  46. #include "addrtoname.h"
  47. #undef MODEMASK                    /* Solaris sucks */
  48. #include "ntp.h"
  49.  
  50. static void p_sfix(const struct s_fixedpt *);
  51. static void p_ntp_time(const struct l_fixedpt *);
  52. static void p_ntp_delta(const struct l_fixedpt *, const struct l_fixedpt *);
  53.  
  54. /*
  55.  * Print ntp requests
  56.  */
  57. void
  58. ntp_print(register const u_char *cp, int length)
  59. {
  60.     register const struct ntpdata *bp;
  61.     register const u_char *ep;
  62.     int mode, version, leapind;
  63.     static char rclock[5];
  64.  
  65. #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
  66.  
  67.     bp = (struct ntpdata *)cp;
  68.     /* Note funny sized packets */
  69.     if (length != sizeof(struct ntpdata))
  70.         (void)printf(" [len=%d]", length);
  71.  
  72.     /* 'ep' points to the end of avaible data. */
  73.     ep = snapend;
  74.  
  75.     TCHECK(bp->status, sizeof(bp->status));
  76.  
  77.     version = (bp->status & VERSIONMASK) >> 3;
  78.     printf(" v%d", version);
  79.  
  80.     leapind = bp->status & LEAPMASK;
  81.     switch (leapind) {
  82.  
  83.     case NO_WARNING:
  84.         break;
  85.  
  86.     case PLUS_SEC:
  87.         fputs(" +1s", stdout);
  88.         break;
  89.  
  90.     case MINUS_SEC:
  91.         fputs(" -1s", stdout);
  92.         break;
  93.     }
  94.  
  95.     mode = bp->status & MODEMASK;
  96.     switch (mode) {
  97.  
  98.     case MODE_UNSPEC:    /* unspecified */
  99.         fputs(" unspec", stdout);
  100.         break;
  101.  
  102.     case MODE_SYM_ACT:    /* symmetric active */
  103.         fputs(" sym_act", stdout);
  104.         break;
  105.  
  106.     case MODE_SYM_PAS:    /* symmetric passive */
  107.         fputs(" sym_pas", stdout);
  108.         break;
  109.  
  110.     case MODE_CLIENT:    /* client */
  111.         fputs(" client", stdout);
  112.         break;
  113.  
  114.     case MODE_SERVER:    /* server */
  115.         fputs(" server", stdout);
  116.         break;
  117.  
  118.     case MODE_BROADCAST:    /* broadcast */
  119.         fputs(" bcast", stdout);
  120.         break;
  121.  
  122.     case MODE_RES1:        /* reserved */
  123.         fputs(" res1", stdout);
  124.         break;
  125.  
  126.     case MODE_RES2:        /* reserved */
  127.         fputs(" res2", stdout);
  128.         break;
  129.  
  130.     }
  131.  
  132.     TCHECK(bp->stratum, sizeof(bp->stratum));
  133.     printf(" strat %d", bp->stratum);
  134.  
  135.     TCHECK(bp->ppoll, sizeof(bp->ppoll));
  136.     printf(" poll %d", bp->ppoll);
  137.  
  138.     /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
  139.     TCHECK(bp->distance, 0);
  140.     printf(" prec %d", bp->precision);
  141.  
  142.     if (!vflag)
  143.         return;
  144.  
  145.     TCHECK(bp->distance, sizeof(bp->distance));
  146.     fputs(" dist ", stdout);
  147.     p_sfix(&bp->distance);
  148.  
  149.     TCHECK(bp->dispersion, sizeof(bp->dispersion));
  150.     fputs(" disp ", stdout);
  151.     p_sfix(&bp->dispersion);
  152.  
  153.     TCHECK(bp->refid, sizeof(bp->refid));
  154.     fputs(" ref ", stdout);
  155.     /* Interpretation depends on stratum */
  156.     switch (bp->stratum) {
  157.  
  158.     case UNSPECIFIED:
  159.     case PRIM_REF:
  160.         strncpy(rclock, (char *)&(bp->refid), 4);
  161.         rclock[4] = '\0';
  162.         fputs(rclock, stdout);
  163.         break;
  164.  
  165.     case INFO_QUERY:
  166.         printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
  167.         /* this doesn't have more content */
  168.         return;
  169.  
  170.     case INFO_REPLY:
  171.         printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
  172.         /* this is too complex to be worth printing */
  173.         return;
  174.  
  175.     default:
  176.         printf("%s", ipaddr_string(&(bp->refid)));
  177.         break;
  178.     }
  179.  
  180.     TCHECK(bp->reftime, sizeof(bp->reftime));
  181.     putchar('@');
  182.     p_ntp_time(&(bp->reftime));
  183.  
  184.     TCHECK(bp->org, sizeof(bp->org));
  185.     fputs(" orig ", stdout);
  186.     p_ntp_time(&(bp->org));
  187.  
  188.     TCHECK(bp->rec, sizeof(bp->rec));
  189.     fputs(" rec ", stdout);
  190.     p_ntp_delta(&(bp->org), &(bp->rec));
  191.  
  192.     TCHECK(bp->xmt, sizeof(bp->xmt));
  193.     fputs(" xmt ", stdout);
  194.     p_ntp_delta(&(bp->org), &(bp->xmt));
  195.  
  196.     return;
  197.  
  198. trunc:
  199.     fputs(" [|ntp]", stdout);
  200. #undef TCHECK
  201. }
  202.  
  203. static void
  204. p_sfix(register const struct s_fixedpt *sfp)
  205. {
  206.     register int i;
  207.     register int f;
  208.     register float ff;
  209.  
  210.     i = ntohs(sfp->int_part);
  211.     f = ntohs(sfp->fraction);
  212.     ff = f / 65536.0;    /* shift radix point by 16 bits */
  213.     f = ff * 1000000.0;    /* Treat fraction as parts per million */
  214.     printf("%d.%06d", i, f);
  215. }
  216.  
  217. #define    FMAXINT    (4294967296.0)    /* floating point rep. of MAXINT */
  218.  
  219. static void
  220. p_ntp_time(register const struct l_fixedpt *lfp)
  221. {
  222.     register int32 i;
  223.     register u_int32 uf;
  224.     register u_int32 f;
  225.     register float ff;
  226.  
  227.     i = ntohl(lfp->int_part);
  228.     uf = ntohl(lfp->fraction);
  229.     ff = uf;
  230.     if (ff < 0.0)        /* some compilers are buggy */
  231.         ff += FMAXINT;
  232.     ff = ff / FMAXINT;    /* shift radix point by 32 bits */
  233.     f = ff * 1000000000.0;    /* treat fraction as parts per billion */
  234.     printf("%lu.%09d", i, f);
  235. }
  236.  
  237. /* Prints time difference between *lfp and *olfp */
  238. static void
  239. p_ntp_delta(register const struct l_fixedpt *olfp,
  240.         register const struct l_fixedpt *lfp)
  241. {
  242.     register int32 i;
  243.     register u_int32 uf;
  244.     register u_int32 ouf;
  245.     register u_int32 f;
  246.     register float ff;
  247.     int signbit;
  248.  
  249.     i = ntohl(lfp->int_part) - ntohl(olfp->int_part);
  250.  
  251.     uf = ntohl(lfp->fraction);
  252.     ouf = ntohl(olfp->fraction);
  253.  
  254.     if (i > 0) {        /* new is definitely greater than old */
  255.         signbit = 0;
  256.         f = uf - ouf;
  257.         if (ouf > uf)    /* must borrow from high-order bits */
  258.             i -= 1;
  259.     } else if (i < 0) {    /* new is definitely less than old */
  260.         signbit = 1;
  261.         f = ouf - uf;
  262.         if (uf > ouf)    /* must carry into the high-order bits */
  263.             i += 1;
  264.         i = -i;
  265.     } else {        /* int_part is zero */
  266.         if (uf > ouf) {
  267.             signbit = 0;
  268.             f = uf - ouf;
  269.         } else {
  270.             signbit = 1;
  271.             f = ouf - uf;
  272.         }
  273.     }
  274.  
  275.     ff = f;
  276.     if (ff < 0.0)        /* some compilers are buggy */
  277.         ff += FMAXINT;
  278.     ff = ff / FMAXINT;    /* shift radix point by 32 bits */
  279.     f = ff * 1000000000.0;    /* treat fraction as parts per billion */
  280.     if (signbit)
  281.         putchar('-');
  282.     else
  283.         putchar('+');
  284.     printf("%d.%09d", i, f);
  285. }
  286.